package nautilus.lab.renderer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView.ScaleType;

public class ShadowImageView extends View{

	private Paint _paint = new Paint();
	private Bitmap _bitmap;
	private Bitmap _framePhoto;
	private Drawable _placeholder;
	private ScaleType _scaleType;
	
	public ShadowImageView(Context context){
		super(context);
		initView();
	}
	
	public ShadowImageView(Context context, AttributeSet attrSet){
		super(context, attrSet);
		initView();
	}
	
	public ShadowImageView(Context context, AttributeSet attrSet, int defStyle){
		super(context, attrSet, defStyle);
		initView();
	}
	
	public ShadowImageView(Context context, Bitmap bm) {
		super(context);
		initView();
		_bitmap = bm;
	}
	
	private void initView(){
		_paint.setShadowLayer(10.0f, 0.0f, 0.0f, 0xff000000);
		_scaleType = ScaleType.FIT_CENTER;
	}
	
	/*
	protected void onMeasure(int widthMeasure, int heightMeasure){
		int measuredWidth = getDefaultSize(size, measureSpec)
	}*/
	
	public void setImageBitmap(Bitmap bitmap){
		_bitmap = bitmap;
	}
	
	public void setDrawable(Drawable drawable){
		_placeholder = drawable;
	}
	
	public ScaleType getScaleType(){
		return _scaleType;
	}
	
	/*
	 * Control how the image, should be resized or moved to match the size
	 * of this view
	 * */
	public void setScaleType(ScaleType scaleType){
		if(scaleType==null)
			throw new NullPointerException();
		
		if(_scaleType != scaleType){
			_scaleType = scaleType;
			
			setWillNotCacheDrawing(_scaleType == ScaleType.CENTER);
			requestLayout();
			invalidate();
		}
	}
	
	@Override
	protected void onDraw(Canvas canvas){
		if(_placeholder==null && _bitmap == null)
			return;
		
		if(_framePhoto == null)
			createFramePhoto();
		
		canvas.drawBitmap(_framePhoto, 0.0f, 0.0f, _paint);
	}
	
	private void createFramePhoto(){
		Drawable imageDrawable = (_bitmap != null)?new BitmapDrawable(getContext().getResources(), _bitmap): _placeholder;
		Bitmap output = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		RectF outerRect = new RectF(0, 0, getWidth(), getHeight());
		float outerRadius = (getWidth()<getHeight()?getWidth():getHeight())/18f;
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.RED);
		canvas.drawRoundRect(outerRect, outerRadius, outerRadius, paint);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		imageDrawable.setBounds(0, 0, getWidth(), getHeight());
		
		//save the layer to apply the paint
		canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
		imageDrawable.draw(canvas);
		canvas.restore();
		
		_framePhoto = output;
	}

}
